package com.whmall.service.crm.module.customerpool.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whmall.component.entity.dto.OperatorReqDTO;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.component.service.AbstractDbService;
import com.whmall.service.crm.entity.dto.customerpool.CustomerPoolFollowRecordCreateDTO;
import com.whmall.service.crm.entity.dto.customerpool.CustomerPoolFollowRecordQueryDTO;
import com.whmall.service.crm.entity.dto.customerpool.CustomerPoolFollowRecordRspDTO;
import com.whmall.service.crm.entity.dto.customerpool.CustomerPoolFollowRecordUpdateDTO;
import com.whmall.service.crm.module.customerpool.dao.CustomerPoolFollowRecordMapper;
import com.whmall.service.crm.module.customerpool.entity.CustomerPoolFollowRecordDO;
import com.whmall.service.crm.module.customerpool.service.CustomerPoolFollowRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class CustomerPoolFollowRecordServiceImpl extends AbstractDbService<CustomerPoolFollowRecordMapper, CustomerPoolFollowRecordDO>
        implements CustomerPoolFollowRecordService {

    @Override
    public PageRspDTO<CustomerPoolFollowRecordRspDTO> page(PageReqDTO pageReqDTO, CustomerPoolFollowRecordQueryDTO queryReqDTO) {
        LambdaQueryWrapper<CustomerPoolFollowRecordDO> queryWrapper = makeCondition(queryReqDTO);

        IPage<CustomerPoolFollowRecordDO> page = super.page(Page.of(pageReqDTO.getPageNum(), pageReqDTO.getPageSize()), queryWrapper);
        if (CollUtil.isEmpty(page.getRecords())) {
            return PageRspDTO.EMPTY;
        }

        return PageRspDTO.of(pageReqDTO, (int) page.getPages(), (int) page.getTotal(), page.getRecords().stream().map(r -> {
            CustomerPoolFollowRecordRspDTO rspDTO = new CustomerPoolFollowRecordRspDTO();
            BeanUtils.copyProperties(r, rspDTO);
            return rspDTO;
        }).collect(Collectors.toList()));
    }

    @Override
    public List<CustomerPoolFollowRecordRspDTO> list(CustomerPoolFollowRecordQueryDTO condition) {
        LambdaQueryWrapper<CustomerPoolFollowRecordDO> queryWrapper = makeCondition(condition);
        List<CustomerPoolFollowRecordDO> list = this.list(queryWrapper);

        return list.stream().map(e -> BeanUtil.copyProperties(e, CustomerPoolFollowRecordRspDTO.class)).collect(Collectors.toList());
    }

    @Override
    public PageRspDTO<CustomerPoolFollowRecordRspDTO> groupByCompanyIdWithTraderId(PageReqDTO pageReqDTO, CustomerPoolFollowRecordQueryDTO condition) {
        PageInfo<Long> pageInfo = PageHelper.startPage(pageReqDTO.getPageNum(), pageReqDTO.getPageSize())
                .doSelectPageInfo(() -> this.baseMapper.groupByCompanyIdWithTraderId(condition));
        if (pageInfo.getList().isEmpty()) {
            return new PageRspDTO<>(
                    pageInfo.getPageNum(),
                    pageInfo.getPageSize(),
                    pageInfo.getPages(),
                    (int) pageInfo.getTotal(),
                    new ArrayList<>()
            );
        }
        condition.setCompanyIds(pageInfo.getList());
        condition.setFollowTimeGte(null);
        condition.setFollowTimeLte(null);
        return new PageRspDTO<>(
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getPages(),
                (int) pageInfo.getTotal(),
                this.baseMapper.listWithTraderId(condition).stream()
                        .map(e -> BeanUtil.copyProperties(e, CustomerPoolFollowRecordRspDTO.class))
                        .collect(Collectors.toList())
        );
    }

    @Override
    public void save(CustomerPoolFollowRecordCreateDTO reqDTO, OperatorReqDTO operatorReqDTO) {
        CustomerPoolFollowRecordDO informationDO = new CustomerPoolFollowRecordDO();
        BeanUtils.copyProperties(reqDTO, informationDO);
        informationDO.setTraderId(operatorReqDTO.getOperatorId());
        informationDO.setTraderDepartmentPath(operatorReqDTO.getDepartmentPath());
        informationDO.setCreateTime(new Date());
        super.baseMapper.insert(informationDO);
    }

    @Override
    public void update(CustomerPoolFollowRecordUpdateDTO reqDTO, OperatorReqDTO operatorReqDTO) {
        Assert.isTrue(null != reqDTO.getId() && null != super.getById(reqDTO.getId()), "请求参数异常");
        CustomerPoolFollowRecordDO informationDO = new CustomerPoolFollowRecordDO();
        BeanUtils.copyProperties(reqDTO, informationDO);
        super.baseMapper.updateById(informationDO);
    }

    /**
     * 组装分页查询条件
     *
     * @param queryReqDTO
     * @return
     */
    private LambdaQueryWrapper<CustomerPoolFollowRecordDO> makeCondition(CustomerPoolFollowRecordQueryDTO queryReqDTO) {
        LambdaQueryWrapper<CustomerPoolFollowRecordDO> queryWrapper = Wrappers.lambdaQuery();
        Map<SFunction<CustomerPoolFollowRecordDO, ?>, Object> map = new HashMap<>();
        map.put(CustomerPoolFollowRecordDO::getCompanyId, queryReqDTO.getCompanyId());
        map.put(CustomerPoolFollowRecordDO::getBusinessType, queryReqDTO.getBusinessType());
        map.put(CustomerPoolFollowRecordDO::getFollowType, queryReqDTO.getFollowType());
        queryWrapper.allEq(map, false);
        queryWrapper.in(CollUtil.isNotEmpty(queryReqDTO.getCompanyIds()), CustomerPoolFollowRecordDO::getCompanyId, queryReqDTO.getCompanyIds());
        queryWrapper.ge(ObjectUtil.isNotNull(queryReqDTO.getFollowTimeGte()), CustomerPoolFollowRecordDO::getFollowTime, queryReqDTO.getFollowTimeGte());
        queryWrapper.le(ObjectUtil.isNotNull(queryReqDTO.getFollowTimeLte()), CustomerPoolFollowRecordDO::getFollowTime, queryReqDTO.getFollowTimeLte());
        queryWrapper.ge(ObjectUtil.isNotNull(queryReqDTO.getTimeGte()), CustomerPoolFollowRecordDO::getCreateTime, queryReqDTO.getTimeGte());
        queryWrapper.le(ObjectUtil.isNotNull(queryReqDTO.getTimeLte()), CustomerPoolFollowRecordDO::getCreateTime, queryReqDTO.getTimeLte());
        queryWrapper.orderByDesc(CustomerPoolFollowRecordDO::getCreateTime);
        return queryWrapper;
    }
}